package com.cx.service.impl;

import com.cx.mapper.OrderGoodsMapper;
import com.cx.mapper.OrdersMapper;
import com.cx.mapper.SeckillGoodsMapper;
import com.cx.pojo.OrderGoods;
import com.cx.pojo.Orders;
import com.cx.pojo.SeckillGoods;
import com.cx.service.OrderService;
import com.cx.service.SeckillGoodsOrder;
import com.cx.service.SeckillVOService;
import com.cx.util.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description
 * @Author 11597
 * @Date 2020/6/28 23:40
 */
@Service
public class SeckillGoodsOrderImpl implements SeckillGoodsOrder {

    @Autowired
    SeckillVOService seckillVOService;

    @Autowired
    SeckillGoodsMapper seckillGoodsMapper;


    @Autowired
    OrderService orderService;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @Transactional
    @Override
    public int addSeckillGoodsOrder(Integer goodId, Integer userId,SeckillGoods seckillGoods) {
        OrderUtil orderUtil = new OrderUtil();
        Orders orders = new Orders();
        OrderGoods orderGoods = new OrderGoods();
        String orderNo = orderUtil.getGeneratID();
        orders.setOrderNo(orderNo);
        orders.setUserId(userId);
        orders.setOrderStatus(-2);
        orders.setTotalMoney(seckillGoods.getSeckillPrice());
        orders.setOrderType(3);
        orders.setIsPay(0);
        orders.setCreateTime(new Date());
        ordersMapper.insert(orders);

        orders = ordersMapper.selectByOrderNo(orderNo);

        orderGoods.setGoodId(goodId);
        orderGoods.setOrderId(orders.getId());
        orderGoods.setGoodNum(1);
        orderGoods.setCreateTime(new Date());
        orderGoodsMapper.insert(orderGoods);

        seckillGoods.setSeckillNum(seckillGoods.getSeckillNum()+1);
        seckillGoods.setSeckillStock(seckillGoods.getSeckillStock()-1);
        seckillGoodsMapper.updateByPrimaryKey(seckillGoods);
        return orders.getId();
    }
}
